﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Core = MS.InformationManagement.Core;

namespace MS.InformationManagement.BLL.DocumentsTemplates
{
    public class DocumentsTemplates
    {

        /// <summary>
        /// Get all templates 
        /// </summary>
        /// <returns>Dictionray with templates in the site TemplateId,TemplateTitle</returns>
        public static Dictionary<int, string> GetAllTemplates()
        { 
            SPQuery query = new SPQuery();
            query.ViewFields = "<FieldRef Name='Title' />"+
                               "<FieldRef Name='ID' />";
            SPListItemCollection templates = SPContext.Current.Web.Lists[GlobalConstants.LIST_BIBLIOTECA_TEMPLATES].GetItems(query);
            return (from item in templates.OfType<SPListItem>()
                    select item).ToDictionary(c => c.ID, c => c.Name);
        }

        /// <summary>
        /// Creates a new document based on a previus selected template id
        /// </summary>
        /// <param name="newDocumentName">Name for the document</param>
        /// <param name="templateId">Id for the selected template</param>
        /// <returns>New SPFile</returns>
        public static SPFile CreateNewDocument(string newDocumentName, int templateId)
        {
            return Core.TemplatesManagement.TemplatesManagement.CreateNewDocumentByTemplate(newDocumentName, templateId);
        }
    }
}
